using UnityEngine;
using System.Collections;

public class WorldObject : MonoBehaviour {
	[HideInInspector]
	public float rayLength;
	private float offset;
	private Vector3 _direction;
	private CollisionDirection _colDirection;
	public Vector3 pos{get;set;}
	public Vector3 moveDirection{get;set;}
	public Vector3 verticalDirection{get;set;}
	public bool isClockwise{get;set;}
	private LayerMask _worldLayer;
	// Use this for initialization
	virtual public void Start () {
		_worldLayer = 1 << 9;
		offset = Mathf.Max(transform.localScale.x / 2, transform.localScale.y / 2) + 0.05f;
		getCastDirection();
		//Debug.Log(gameObject.name + " direction:" + _direction + ", colDirct:" + _colDirection);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public bool IsMainCharacterPassingByRange()
	{
		RaycastHit hit;
		 if(Physics.Raycast(transform.position - offset * _direction, _direction, out hit, Mathf.Max(2 * rayLength, 0.5f), _worldLayer))
  		{
               if(hit.collider.gameObject.tag == GlobalInfo.MAIN_TAG){
					MainCharacterController mc = hit.transform.gameObject.GetComponent<MainCharacterController>();
					if(mc != null && (mc.curDirection == CollisionDirection.None || mc.curDirection == _colDirection)){
						moveDirection = mc.moveDirection;
						verticalDirection = mc.verticalDirection;
						isClockwise = mc.isClockwise;
						return true;
					}	
				}
        } 
		return false;
	}
	
	public bool IsMainCharacterPassing()
    {
        RaycastHit hit;
		if(Physics.Raycast(transform.position, Vector3.left, out hit,  transform.localScale.x)
			|| Physics.Raycast(transform.position, Vector3.right, out hit,  transform.localScale.x)
			|| Physics.Raycast(transform.position, Vector3.up, out hit, transform.localScale.x)
			|| Physics.Raycast(transform.position, Vector3.down, out hit, transform.localScale.x)){
               if(hit.transform.tag == GlobalInfo.MAIN_TAG)
				return true;
        }
        return false;
    }
	
	private void getCastDirection()
	{
		float zRot = (transform.rotation.eulerAngles.z + 360.0f) % 360.0f;
		float zOffset = 5.0f;
		if(Mathf.Abs(zRot - 180.0f) < zOffset){
			_direction = Vector3.up;
			_colDirection = CollisionDirection.Down;
			return;
		}
		if(Mathf.Abs(zRot - 270.0f) < zOffset){
			_direction = Vector3.left;
			_colDirection = CollisionDirection.Right;
			return;
		}
		if(Mathf.Abs(zRot - 90.0f) < zOffset){
			_direction = Vector3.right;
			_colDirection = CollisionDirection.Left;
			return;
		}
		if(Mathf.Abs(zRot - 0.0f) < zOffset){
			_direction = Vector3.down;
			_colDirection = CollisionDirection.Down;
			return;
		}
//		Vector3 castDrct;
//		castDrct = Vector3.up;
//		if(Physics.Raycast(transform.position, castDrct, transform.localScale.x))
//  		{
//             _direction = -castDrct;
//			_colDirection = CollisionDirection.Up;
//			return;
//        } 
//		
//		castDrct = Vector3.down;
//		if(Physics.Raycast(transform.position, castDrct, transform.localScale.x))
//  		{
//             _direction = -castDrct;
//			_colDirection = CollisionDirection.Down;
//			return;
//        } 
//		
//		castDrct = Vector3.left;
//		if(Physics.Raycast(transform.position, castDrct, transform.localScale.x))
//  		{
//             _direction = -castDrct;
//			_colDirection = CollisionDirection.Left;
//			return;
//        } 
//		
//		castDrct = Vector3.right;
//		if(Physics.Raycast(transform.position, castDrct, transform.localScale.x))
//  		{
//             _direction = -castDrct;
//			_colDirection = CollisionDirection.Right;
//			return;
//        } 
		_colDirection = CollisionDirection.None;
	}
}
